package cn.xuchunh.campusbbs.utils;

import android.text.TextUtils;

import java.awt.font.TextAttribute;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;

/**
 * Created by XuChunH on 2015/8/31.
 */
public class StringUtil {

    public static String replace(String oldString, int start, int end, String newString) {
        if (TextUtils.isEmpty(oldString) || newString == null) {
            return "";
        }

        if (start < 0 || start > oldString.length() - 1 || end < 0 || end > oldString.length() || end <= start) {
            return oldString;
        }
        return oldString.substring(start, end) + newString + oldString.substring(end - 1);
    }

    public static String formatFileSize(long size) {
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        if (size < 1024) {
            return size + " B";
        } else if (size < 1024 * 1024) {
            return decimalFormat.format((double) size / 1024) + " KB";
        } else if (size < 1024 * 1024 * 1024) {
            return decimalFormat.format((double) size / (1024 * 1024)) + " MB";
        }
        return "";
    }

    public static String getNameFromUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return "";
        }
        int index = url.lastIndexOf("/");
        String name = "";
        if (index != -1 && index != url.length() - 1) {
            name = url.substring(index);
        }
        return name;

    }

    /**
     * 将字符串转成MD5值
     */
    public static String stringToMD5(String string) {
        byte[] hash;

        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10) {
                hex.append("0");
            }
            hex.append(Integer.toHexString(b & 0xFF));
        }

        return hex.toString();
    }

    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len; ) {
            aChar = theString.charAt(x++);
            if (aChar == '\\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                            case '0':
                            case '1':
                            case '2':
                            case '3':
                            case '4':
                            case '5':
                            case '6':
                            case '7':
                            case '8':
                            case '9':
                                value = (value << 4) + aChar - '0';
                                break;
                            case 'a':
                            case 'b':
                            case 'c':
                            case 'd':
                            case 'e':
                            case 'f':
                                value = (value << 4) + 10 + aChar - 'a';
                                break;
                            case 'A':
                            case 'B':
                            case 'C':
                            case 'D':
                            case 'E':
                            case 'F':
                                value = (value << 4) + 10 + aChar - 'A';
                                break;
                            default:
                                throw new IllegalArgumentException(
                                        "Malformed   \\uxxxx   encoding.");
                        }

                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't') {
                        aChar = '\t';
                    } else if (aChar == 'r') {
                        aChar = '\r';
                    } else if (aChar == 'n') {
                        aChar = '\n';
                    } else if (aChar == 'f') {
                        aChar = '\f';
                    } else if (aChar == '\"') {
                        aChar = '\"';
                        outBuffer.append("\\");
                    } else if (aChar == '\\') {
                        aChar = '\\';
                        outBuffer.append('\\');
                    }
                    outBuffer.append(aChar);
                }
            } else {
                outBuffer.append(aChar);
            }
        }
        return outBuffer.toString();
    }

}
